package com.elong.android.specialhouse.ui;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;

import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.adapter.HomeAdsGalleryAdapter;
import com.elong.android.specialhouse.entity.AdImageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class HomePageAdsView extends FrameLayout implements OnItemClickListener, OnItemSelectedListener, View.OnTouchListener {

	private static final String TAG = "HomePageAdsView";

	// private HomeActivity mHomeActivity;
	private OnAdsItemClickListener mOnAdsItemClickListener;
	private Context mContext;
	public PagedGallery mAdsGallery;

	private View mRealAdvsContent;
	private View mDefaultAdvContent;
	private IndexDotsView mIndexDots;

	public static final int TIME_INTEVAL_REFRESH_ADS = 3000;
	public static final int TIME_REFRESH_ADS_DELAY = 3000;
	public static final int TIME_INTERVAL_DOWNLOAD_IMAGE_FAILED = 5000;

	private boolean mIsRealAds = false;

	private List<AdImageInfo> mAdvs;

//	private Timer mTimer;
	public static final int MSG_REFRESH_ADS = 0;
	public static final int MSG_INVALIDATE_ADS_WITH_REAL_ADS = 1;

//	private TimerTask refshAdsTask;
	private HomeAdsGalleryAdapter mAdapter = null;
	protected ImageLoader m_imageLoader = ImageLoader.getInstance();

	private Handler mHandler = new Handler() ;

	public HomePageAdsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	//
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mRealAdvsContent = findViewById(R.id.real_ads);
		mDefaultAdvContent = findViewById(R.id.iv_default_ads);

		mAdsGallery = (PagedGallery) findViewById(R.id.ads_container);
		mIndexDots = (IndexDotsView) findViewById(R.id.dot_position);

	}

	public void invalidateAdsWithReadAds(List<AdImageInfo> advs) {

		mAdvs = advs;

		mIndexDots.initViews(advs.size(), 0);
		mAdapter = new HomeAdsGalleryAdapter(advs);
		mAdsGallery.setAdapter(mAdapter);

		mAdapter.notifyDataSetChanged();
		// 为了解决首页广告在第一次往左切换时无法切换的问题
//		mAdsGallery.setSelection(advs.size() * 10);
		mAdsGallery.setOnItemClickListener(this);
		mAdsGallery.setOnItemSelectedListener(this);
		mAdsGallery.setOnTouchListener(this);

		mIsRealAds = true;
		mRealAdvsContent.setVisibility(View.VISIBLE);
		mDefaultAdvContent.setVisibility(View.GONE);

		startAutorefreshAds();

	}

	public void startAutorefreshAds() {
		// 广告数量必须超过2才会开启自动轮播功能
		if (mAdvs == null || mAdvs.size() < 2) {
			return;
		}
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				int selection = mAdsGallery.getSelectedItemPosition() + 1;
				mAdsGallery.setSelection(selection);
				mHandler.postDelayed(this, TIME_REFRESH_ADS_DELAY);
			}
		},TIME_REFRESH_ADS_DELAY);

//		if (null == mTimer) {
//			mTimer = new Timer();
//			refshAdsTask = new TimerTask() {
//				@Override
//				public void run() {
//					mHandler.obtainMessage(MSG_REFRESH_ADS).sendToTarget();
//				}
//			};
//			mTimer.schedule(refshAdsTask, TIME_REFRESH_ADS_DELAY, TIME_INTEVAL_REFRESH_ADS);
//		}

	}

	public void stopAutoRefreshAds() {
		mHandler.removeCallbacksAndMessages(null);
//		if (null != refshAdsTask) {
//			refshAdsTask.cancel();
//			refshAdsTask = null;
//		}
//
//		if (null != mTimer) {
//			mTimer.cancel();
//			mTimer = null;
//		}
	}

	public void onPause() {
		stopAutoRefreshAds();
	}

	public void onResume() {
		if (mIsRealAds) {
			startAutorefreshAds();
		}
	}

	public void setOnAdsItemClickListener(OnAdsItemClickListener onAdsItemClickListener) {
		this.mOnAdsItemClickListener = onAdsItemClickListener;
	}

	public OnAdsItemClickListener getOnAdsItemClickListener() {
		return mOnAdsItemClickListener;
	}

	public interface OnAdsItemClickListener {
		void onAdsItemClick(Context mContext, List<AdImageInfo> mAdvs, int position);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		mOnAdsItemClickListener.onAdsItemClick(mContext, mAdvs, position % mAdvs.size());

		// if (!IConfig.CLICKABLE_MONKEY_ADS) {
		// return;
		// }
		// if (mAdvs.size() > 0) {
		// // 为了实现广告条的无限循环效果,取偏移量
		// String jumpLink = mAdvs.get(position % mAdvs.size()).getJumpLink();
		// // test purpose
		// // Around around = mAdvs.get(position%mAdvs.size());
		// // sendHttpException2MonitorServer
		// AdvertisementInfo around = mAdvs.get(position % mAdvs.size());
		// TabHomeActivity.COUNTLY_ROOT_TEXT = "adbanner";

		// Utils.handleAdvClick(mHomeActivity, around, jumpLink);
		// }
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
		if (mAdvs == null || mAdvs.size() < 1) {
			return;
		}

		// 为了实现广告条的无限循环效果,取偏移量
		position = position % mAdvs.size();
		mIndexDots.setCurrent(position);
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			stopAutoRefreshAds();
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			startAutorefreshAds();
		}
		return super.onTouchEvent(event);
	}
}
